package cn.xiaobai.config;

import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * @Author yangdaji
 * @Date 2023/12/23
 * @PackageName cn.xiaobai.config
 * @ClassName MysqlConfig
 */
public class MysqlConfig {

    private static Logger log = Logger.getLogger(MysqlConfig.class.getName());

    private final static String DB_FILE = "/assets/db.properties";

    private String url;
    private String username;
    private String password;


    public static MysqlConfig getInstance() {
        MysqlConfig mysqlConfig = new MysqlConfig();
        mysqlConfig.dataSourceConfig();
        return mysqlConfig;
    }

    private void dataSourceConfig() {
        Properties properties = new Properties();
        InputStream resourceAsStream = this.getClass().getResourceAsStream(DB_FILE);
        try {
            if (resourceAsStream != null) {
                properties.load(resourceAsStream);
                config(properties);
            }
        } catch (Exception e) {
            log.warning("db.properties load fail / not found! ");
        }

    }

    private void config(Properties properties) {
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
    }


    public String getUrl() {
        return url;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

}
